<script language="javascript">
	// chargement tableau
	var tache_list = new Array();
	var tri = '';
	var inverse = false;
	
	<?php
		function extractTagAtt($tag, $att) {
			if (preg_match("/$att=\"([^\"]+)\"/", $tag, $match) > 0)
				return $match[1];
			else 
				return '';
		}
		
		function populate($tache) {
			$ordre = (($tache->getTacDateEcheance() <> '' ? format_date($tache->getTacDateEcheance(), 'yyyyMMdd') : 20991231) - date('Ymd') +1)*5 - $tache->getTacUrgence();
			
			switch ($tache->getTacUrgence()) {
				case TacheTable::TAC_URG_URGENT:
					$urgence = image_tag('urg1.png');
					break;
				case TacheTable::TAC_URG_IMMEDIAT:
					$urgence = image_tag('urg2.png');
					break;
				default:
					$urgence = '';
			}
			
			if ($tache->isRetard()) {
				$retard = image_tag('retard.png');
			} elseif ($tache->isJour()) {
				$retard = image_tag('jour.png');
			} else
				$retard = '';

			echo "var tache = new Array();\n";
			echo "tache['id'] = ".$tache->getTacId().";\n";
			echo "tache['nb_fils'] = ".$tache->nbFils().";\n";
			echo "tache['show_href'] = '". url_for('tache/show?tac_id='.$tache->getTacId()) ."';\n";
			echo "tache['show_img_src'] = '". extractTagAtt(image_tag('show.png'), 'src') ."';\n";
			echo "tache['ordre'] = $ordre;\n";
			echo "tache['retard'] = '".$tache->isRetard()."';\n";
			echo "tache['retard_img_src'] = '".extractTagAtt($retard, 'src')."';\n";
			echo "tache['categorie'] = '".$tache->getCategorie()."';\n";
			echo "tache['libelle'] = '".$tache->getTacLibelle()."';\n";
			echo "tache['urgence_src'] = '". extractTagAtt($urgence, 'src') ."';\n";
			echo "tache['etat'] = '".$tache->getEtat()."';\n";
			echo "tache['date_debut'] = '".$tache->getTacDateDebut()."';\n";
			echo "tache['date_debut_formate'] = '".format_date($tache->getTacDateDebut(), 'dd/MM/yyyy')."';\n";
			echo "tache['date_echeance'] = '".($tache->getTacDateEcheance() <> '' ? $tache->getTacDateEcheance() : 20991231)."';\n";
			echo "tache['date_echeance_formate'] = '".($tache->getTacDateEcheance() <> '' ? format_date($tache->getTacDateEcheance(), 'dd/MM/yyyy') : '')."';\n";
			echo "tache['charges'] = '".sprintf('%.2f + %.2f / %.2f', $tache->getTacChargeConsommee(), $tache->getTacChargeRestante(), $tache->getTacChargeEstimee())."';\n";
			echo "tache['avancement'] = '".sprintf('%d%%', round($tache->getAvancement()*100, 0))."';\n";
			echo "tache['depassement'] = '".sprintf('%d%%', round($tache->getDepassement()*100, 0))."';\n";
			echo "tache['hidden'] = false;\n";
			echo "tache_list[tache_list.length] = tache;\n";
		}
		
		foreach ($tache_list as $tache) {
			populate($tache);
		}
	?>
	
	function affichage(ordre_tri) {
		function appendTD(text, td_class) {
			var td = document.createElement("TD")
			td.appendChild(document.createTextNode(text))
			if (td_class != null) {
				td.setAttribute('class', td_class);
			}
			row.appendChild(td);
		}
		function appendTDIMG(src, td_class) {
			var td = document.createElement("TD")
			if (src != '') {
				var img = document.createElement('IMG');
				img.setAttribute('src', src);
				td.appendChild(img)
			}
			if (td_class != null) {
				td.setAttribute('class', td_class);
			}
			row.appendChild(td);
		}
		
		if (ordre_tri == tri)
			inverse = !inverse;
		else
			inverse = false;
		tri = ordre_tri;
		tache_list.sort(sortfunction);
		var tbody = document.getElementById('table_taches').getElementsByTagName('TBODY')[0];
		// suppression des lignes existantes
		for (var i=tbody.childNodes.length-1; i>=0; i--) {
			tbody.removeChild(tbody.childNodes[i]);
		}
		
		// ajout des nouvelles lignes triées
		var ind = 0;
		for (var i=0; i<tache_list.length; i++) {
			if (!tache_list[i]['hidden']) {
				var row = document.createElement("TR")
				row.setAttribute('class', (ind % 2 == 1 ? 'impair' : 'pair'));
				
				var td = document.createElement("TD")
				var a = document.createElement('A');
				var img = document.createElement('IMG');
				a.setAttribute('href', tache_list[i]['show_href']);
				img.setAttribute('src', tache_list[i]['show_img_src']);
				a.appendChild(img);
				td.appendChild(a)
				td.setAttribute('class', 'tache');
				row.appendChild(td);
				
				// autres colonnes
				appendTDIMG(tache_list[i]['retard_img_src'], 'tache');
				appendTD(tache_list[i]['categorie']);
				appendTD(tache_list[i]['libelle']);
				if (tache_list[i]['nb_fils'] > 0) {
					appendTD('+');
					// index du père
				} else {
					appendTD('');
				}
				appendTDIMG(tache_list[i]['urgence_src']);
				appendTD(tache_list[i]['etat']);
				appendTD(tache_list[i]['date_debut_formate']);
				appendTD(tache_list[i]['date_echeance_formate']);
				appendTD(tache_list[i]['charges']);
				appendTD(tache_list[i]['avancement']);
				appendTD(tache_list[i]['depassement']);
				tbody.appendChild(row);
				ind++;
			}
		}
	}
	
	function sortfunction(a, b){
		//Compare "a" and "b" in some fashion, and return -1, 0, or 1
		if (!inverse)
			return (a[tri] > b[tri] ? 1 : (a[tri] == b[tri] ? 0 : -1));
		else
			return (a[tri] > b[tri] ? -1 : (a[tri] == b[tri] ? 0 : 1));
	}

	function find() {
		var filtre = prompt ("Filtre :",".*");
		for (var i=0; i<tache_list.length; i++) {
			var re = new RegExp(filtre, 'i');
			if (tache_list[i]['libelle'].match(re) == null) {
				tache_list[i]['hidden'] = true;
			}
		}
		affichage('libelle');
	}
</script>

<table id="table_taches" class="liste">
	<thead>
		<tr>
			<th></th>
			<th></th>
<!-- ### ajout des commandes de tri onClick=affichage(colonne_triée) -->
			<th><?php echo image_tag('tri.png', array('onClick' => "affichage('categorie');")); ?>Catégorie</th>
			<th><?php echo image_tag('tri.png', array('onClick' => "affichage('libelle');")); ?>Tache <span onClick="find()">+</span></th>
			<th/>
			<th>Urgence</th>
			<th>Etat</th>
			<th><?php echo image_tag('tri.png', array('onClick' => "affichage('date_debut');")); ?>Début</th>
			<th><?php echo image_tag('tri.png', array('onClick' => "affichage('date_echeance');")); ?>Echéance</th>
			<th>Charges</th>
			<th>Avancement</th>
			<th>Dépassement</th>
		</tr>
	</thead>
	<tbody>
		<!-- renseigné via javascript -->
	</tbody>
</table>

<script language="javascript">
	affichage('ordre');
</script>
